package com.amumu.netctoss.dao.impl;

import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import com.amumu.netctoss.dao.RoleDao;
import com.amumu.netctoss.entity.Role;

@Repository
@Scope("prototype")
public class HibernateRoleDao extends HibernateBaseDao implements RoleDao{

	public void delete(int id) throws Exception {
		Role role = findById(id);
		getHibernateTemplate().delete(role);
		
	}

	public List<Role> findAll() throws Exception {
		//from Role r join fetch r.pid
		return (List<Role>) getHibernateTemplate().find("from Role");
	}

	public List<Role> findByAdminId(int id) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public Role findById(int id) throws Exception {
		return (Role) getHibernateTemplate().load(Role.class, id);
	}

	public List<Role> getList(int page, int size) throws Exception {
		return findByPage(page, size, "from Role r join fetch r.pid", null);
	}

	public int getPageCount(int size) throws Exception {
		return getTotalPage(size, "select count (*) from Role", null);
	}

	public void save(Role role) throws Exception {
		getHibernateTemplate().save(role);
		
	}

	public void update(Role role) throws Exception {
		Role r = findById(role.getId());
		r.setName(role.getName());
		r.setPid(role.getPid());
		getHibernateTemplate().update(r);
		
	}
	
}
